//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  MeetingWaitBoard.m
//  iHeima
//
//  Created by he chao on 5/28/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "MeetingWaitBoard.h"
#import "ChatBoard.h"

#pragma mark -

@interface MeetingWaitBoard()
{
	//<#@private var#>
}
@end

@implementation MeetingWaitBoard

DEF_SIGNAL(BEGIN)
DEF_SIGNAL(INVITE)

#define kMessageTime 10

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"等待加入";
    self.navigationController.navigationBarHidden = NO;
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    [self showNaviBar];
    [self loadContent];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:kMessageTime target:self selector:@selector(getData) userInfo:nil repeats:YES];
    [timer fire];
    
//    [self getTeamList];
//    [self getTeamStatus];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //[timer fire];
    
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
    [timer invalidate];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(getTeamStatus) object:nil];
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.stack popBoardAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

ON_SIGNAL2(MeetingWaitBoard, signal)
{
    if ([signal is:MeetingWaitBoard.BEGIN]) {
//        ChatBoard *board = [[ChatBoard alloc] init];
//        board.isMeeting = YES;
//        board.isAdmin = YES;
//        [self.stack pushBoard:board animated:YES];
        if (self.arrayMember.count<=1) {
            [[BeeUITipsCenter sharedInstance] presentMessageTips:@"需要至少一名其他成员加入才能开始私董会"];
            return;
        }
        BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apichat/startboard",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"bid",self.dictMeeting[@"id"]);
        request.tag = 9529;
    }
    else if ([signal is:MeetingWaitBoard.INVITE]){
        [self.stack popBoardAnimated:YES];
    }
}

- (void)getData{
    [self getTeamList];
    if (!self.isAdmin) {
        //[self getTeamStatus];
        [self performSelector:@selector(getTeamStatus) withObject:nil afterDelay:10];
    }
    
}

- (void)getTeamList{
    BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apichat/listboarduser",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"bid",self.dictMeeting[@"id"]);
    request.tag = 9527;
}

- (void)getTeamStatus{//检查会议是否已经开始
    BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apichat/getgroupstatus",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"bid",self.dictMeeting[@"id"]);
    request.tag = 9528;
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"%@",json);
        switch ([json[@"status"] intValue]) {
            case 1:
            {
                switch (request.tag) {
                    case 9527:
                    {
                        if ([json[@"res"] isKindOfClass:[NSArray class]]) {
                            self.arrayMember = json[@"res"];
                            for (int i = 0; i < self.arrayMember.count; i++) {
                                NSString *strUrl = [NSString stringWithFormat:@"%@avatar.php?uid=%@&size=middle",kPicUrl,self.arrayMember[i][@"uid"]];
                                [avatar[i] setImageWithURL:strUrl placeholderImage:IMAGESTRING(@"default_avatar")];
                            }
                        }
                    }
                        break;
                    case 9528:
                    {
                        //1为未开始，2为已开始，3为已结束
                        NSLog(@"%@",json[@"res"]);
                        switch ([json[@"res"] intValue]) {
                            case 1:
                            {
                            }
                                break;
                            case 2:
                            {
                                ChatBoard *board = [[ChatBoard alloc] init];
                                board.isAdmin = NO;
                                board.isMeeting = YES;
                                board.arrayMeetingMember = self.arrayMember;
                                board.dictMeeting = self.dictMeeting;
                                [self.stack pushBoard:board animated:YES];
                            }
                                break;
                                
                            default:
                                break;
                        }
                    }
                        break;
                    case 9529:
                    {
                        ChatBoard *board = [[ChatBoard alloc] init];
                        board.isMeeting = YES;
                        board.isAdmin = self.isAdmin;
                        board.arrayMeetingMember = self.arrayMember;
                        board.dictMeeting = self.dictMeeting;
                        [self.stack pushBoard:board animated:YES];
                    }
                        break;
                        
                    default:
                        break;
                }
                
                
            }
                break;
        }
    }
}

- (void)loadContent{
    BeeUIImageView *imgPersonBg = [BeeUIImageView spawn];
    imgPersonBg.frame = CGRectMake(0, 0, 237.5, 268);
    imgPersonBg.center = CGPointMake(self.viewWidth/2.0, 200);
    imgPersonBg.image = IMAGESTRING(@"meeting_waiting");
    imgPersonBg.userInteractionEnabled = YES;
    [self.view addSubview:imgPersonBg];
    
    BaseLabel *lb = [BaseLabel initLabel:@"如何进行\n团队建设" font:FONT(14) color:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter];
    lb.frame = imgPersonBg.bounds;
    [imgPersonBg addSubview:lb];
    
    NSArray *x = @[@"94",@"181.3",@"179.5",@"94",@"8.5",@"8"];
    NSArray *y = @[@"10",@"61.8",@"158",@"211",@"158",@"62.5"];
    for (int i = 0; i < 6; i++) {
        //AvatarView *avatar = [AvatarView initFrame:{{1,2},{47.5,47.5}} image:nil borderColor:[UIColor clearColor]];
        avatar[i] = [AvatarView initFrame:CGRectMake([x[i] floatValue], [y[i] floatValue], 47.5, 47.5) image:nil borderColor:[UIColor clearColor]];
        //avatar[i].backgroundColor = [UIColor blackColor];
        [imgPersonBg addSubview:avatar[i]];
    }
    
    if (self.isAdmin) {
        BaseButton *btn = [BaseButton initBaseBtn:IMAGESTRING(@"btn1") highlight:IMAGESTRING(@"btn1_pre") text:@"开始私董会" textColor:[UIColor whiteColor] font:FONT(14)];
        btn.frame = CGRectMake(65, imgPersonBg.bottom+20, 190, 44);
        [btn addSignal:MeetingWaitBoard.BEGIN forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
        BaseButton *btnInvite = [BaseButton initBaseBtn:IMAGESTRING(@"invite_member") highlight:nil];
        btnInvite.frame = CGRectMake(115, btn.bottom+10, 89, 40);
        [btnInvite addSignal:MeetingWaitBoard.INVITE forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btnInvite];
    }
    else {
        BaseLabel *lb2 = [BaseLabel initLabel:@"请稍等，有两人以上加入即可开始私董会" font:FONT(14) color:kBlueColor textAlignment:NSTextAlignmentCenter];
        lb2.frame = CGRectMake(0, imgPersonBg.bottom+40, self.viewWidth, 20);
        [self.view addSubview:lb2];
    }
    
    
}

@end
